home *** CD-ROM | disk | FTP | other *** search
/ Risc World 3 / Risc World 3.iso / SOFTWARE / ISSUE6 / PD / PDF / DrawFile / c++ / GuiDrawFile < prev    next >
Text File  |  2003-02-14  |  7KB  |  236 lines

  1. //--------------------------------------------------------------------------
  2. //
  3. //   Copyright (c) 2002, Colin Granville
  4. //
  5. //   All rights reserved.
  6. //
  7. //   Redistribution and use in source and binary forms, with or
  8. //   without modification, are permitted provided that the following 
  9. //   conditions are met:
  10. //
  11. //      * Redistributions of source code must retain the above copyright 
  12. //        notice, this list of conditions and the following disclaimer.
  13. //
  14. //      * Redistributions in binary form must reproduce the above 
  15. //        copyright notice, this list of conditions and the following 
  16. //        disclaimer in the documentation and/or other materials 
  17. //        provided with the distribution.
  18. //
  19. //      * The name Colin Granville may not be used to endorse or promote 
  20. //        products derived from this software without specific prior 
  21. //        written permission.
  22. //
  23. //   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 
  24. //   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 
  25. //   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 
  26. //   FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 
  27. //   COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 
  28. //   INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 
  29. //   (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 
  30. //   SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 
  31. //   HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 
  32. //   STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 
  33. //   ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 
  34. //   OF THE POSSIBILITY OF SUCH DAMAGE.
  35. //
  36. //--------------------------------------------------------------------------
  37.  
  38. #include "GuiDrawFile.h"
  39. #include <string.h>
  40. #include <limits.h>
  41. #include "GuiDrawFileRender.h"
  42. #include "iostream.h"
  43.  
  44. class GuiDrawFileHeader
  45. {
  46.   public:
  47.     GuiDrawFileHeader(const char* progIdentity);
  48.     int           draw;
  49.     int           majorFormatVersion;
  50.     int           minorFormatVersion;
  51.     char          programIdentity[12];
  52.     GuiBBox       bounds;
  53. };
  54.  
  55. GuiDrawFileHeader::GuiDrawFileHeader(const char* progIdentity)
  56.  : draw(*((int*)"Draw")),
  57.    majorFormatVersion(201),
  58.    minorFormatVersion(0)
  59. {
  60.   strncpy(programIdentity, progIdentity,11);
  61.   programIdentity[11]=0;
  62. }
  63.  
  64. //***************************************************************
  65. //***************************************************************
  66. //***************************************************************
  67.  
  68. GuiDrawFile::GuiDrawFile(const char* progName)
  69.  : programName(progName)
  70. {
  71.   reopen();
  72. }
  73.  
  74. //***************************************************************
  75.  
  76. GuiDrawFile::~GuiDrawFile()
  77. {
  78. }
  79.  
  80. //***************************************************************
  81.  
  82. inline void GuiDrawFile::setBBox() {((GuiDrawFileHeader*)getPtr(0))->bounds=bounds;}
  83. inline void GuiDrawFile::setBBox() const {((GuiDrawFileHeader*)getPtr(0))->bounds=bounds;}
  84.  
  85. //***************************************************************
  86.  
  87. void GuiDrawFile::reopen()
  88. {
  89.   size=0;
  90.   capacity=0;
  91.   currentObject=npos;
  92.   failed=0;
  93.  
  94.   data.clear();
  95.   int pos=allocate(sizeof(GuiDrawFileHeader));
  96.   if (failed) {failed |= FILE_FAILED;return;}
  97.   GuiDrawFileHeader header(programName.c_str());
  98.   *((GuiDrawFileHeader*)getPtr(pos))=header;
  99.   bounds(INT_MAX,INT_MAX,INT_MIN,INT_MIN);
  100. }
  101.  
  102. //***************************************************************
  103.  
  104. _kernel_oserror* GuiDrawFile::save(const char* filename)
  105. {
  106.   if (failed & FILE_FAILED) return 0;
  107.   setBBox();
  108.   return _swix(OS_File,_INR(0,5),10,filename,0xAff,0,getPtr(0),getPtr(size));
  109. }
  110.  
  111. //***************************************************************
  112.  
  113. _kernel_oserror* GuiDrawFile::render(const GuiTransform* trans, const GuiBBox* clip, bool toPrinter) const
  114. {
  115.   if (failed & FILE_FAILED) return 0;
  116.   setBBox();
  117.   return GuiDrawFile_render(*this,(DrawFileVisitor::Transform*)trans,clip,toPrinter);
  118. }
  119.  
  120. //***************************************************************
  121.  
  122. _kernel_oserror* GuiDrawFile::getBBox(GuiBBox& bounds,const GuiTransform* trans) const
  123. {
  124.   if (failed & FILE_FAILED) return 0;
  125.   setBBox();
  126.   //GuiDrawFile_BBox
  127.   return _swix(0x45541,_INR(0,4),0,getPtr(0),size,trans,&bounds);
  128. }
  129.  
  130. //***************************************************************
  131.  
  132. _kernel_oserror* GuiDrawFile::declareFonts(bool dont_download) const
  133. {
  134.   if (failed & FILE_FAILED) return 0;
  135.   setBBox();
  136.   //GuiDrawFile_DeclareFonts
  137.   return _swix(0x45542,_INR(0,2),(dont_download ? 1:0) ,getPtr(0),size);
  138. }
  139.  
  140. //***************************************************************
  141.  
  142. void GuiDrawFile::ensureMem(unsigned int size)
  143. {
  144.   //make sure there will be 64k free after allocation
  145.   if (gflex()->availableMemory() < size+64*1024) failed |= OBJECT_FAILED;
  146. //  size+=(64*1024);
  147. //  unsigned int oldSize=0;
  148. //  unsigned int newSize=0;
  149. //  _kernel_oserror* err=_swix(Wimp_SlotSize,_INR(0,1) |_OUT(0),-1,-1,&oldSize);
  150. //  if (!err) err= _swix(Wimp_SlotSize,_INR(0,1) |_OUT(0),oldSize+size,-1,&newSize);
  151. //  if (!err) _swix(Wimp_SlotSize,_INR(0,1),oldSize,-1);
  152. //  if (err || newSize-oldSize < size) failed |= OBJECT_FAILED;
  153. }
  154.  
  155. //***************************************************************
  156.  
  157. size_t GuiDrawFile::allocate(size_t block_size)
  158. {
  159.   if (failed) return npos;
  160.  
  161.   if (size+block_size>capacity)
  162.   {
  163.     //add 4k buffer
  164.     int newsize=((block_size+3)&~3)+4*1024;
  165.     ensureMem(newsize);
  166.     if (hasFailed()) return npos;
  167.  
  168.     if (!data.extend(size+newsize))
  169.     {
  170.       failed |= OBJECT_FAILED;
  171.       return npos;
  172.     }
  173.     capacity=data.size();
  174.   }
  175.   if (size+block_size<=capacity)
  176.   {
  177.     int old_size=size;
  178.     size+=block_size;
  179.     return old_size;
  180.   }
  181.   failed |= OBJECT_FAILED;
  182.   return npos;
  183. }
  184.  
  185. //***************************************************************
  186.  
  187. bool GuiDrawFile::extend(size_t block_size)
  188. {
  189.   if (failed) return 0;
  190.   size_t old_capacity=capacity;
  191. //add 4k buffer
  192.   int newsize=((block_size+3)&~3)+4*1024;
  193.   ensureMem(newsize);
  194.   if (hasFailed() || !data.extend(capacity+newsize))
  195.   {
  196.     failed |= OBJECT_FAILED;
  197.     if (currentObject!=npos)
  198.     {
  199.        data.resize(currentObject);
  200.        capacity=data.size();
  201.        size=capacity;
  202.        return 0;
  203.     }
  204.   }
  205.   capacity=data.size();
  206.   return capacity>=old_capacity+block_size;
  207. }
  208.  
  209. //***************************************************************
  210.  
  211. size_t GuiDrawFile::startObject(size_t s)       
  212. {
  213.   endCurrentObject();
  214.   align();
  215.   currentObject=allocate(s);
  216.   if (currentObject==npos) currentObject=size;
  217.   return currentObject;
  218. }
  219.  
  220. //***************************************************************
  221.  
  222. void GuiDrawFile::endObject() 
  223. {
  224.   if (!failed) currentObject=npos;
  225. }
  226.  
  227. //***************************************************************
  228.  
  229. void GuiDrawFile::endCurrentObject()
  230. {
  231.   if (currentObject!=npos) size=currentObject;
  232.   failed &= ~OBJECT_FAILED;
  233.   currentObject=npos;
  234. }
  235.  
  236.